/*
 * Created on 4 janv. 2007
 */
package fr.mywiki.model.ejbAdapter.entity;

import java.util.Date;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;

import fr.mywiki.business.user.User;
import fr.mywiki.business.versioning.VersionLite;


/**
 * @ejb.bean name="Version"
 *		local-jndi-name="ejb/MyWiki/Version"
 *		view-type="local"
 *		cmp-version="2.x"
 *		primkey-field="id"
 *		schema="Version"
 *
 * @ejb.persistence table-name="version"
 * 
 * @ejb.finder view-type="local"
 *		signature="java.util.Collection findAll()"
 *		unchecked="true"
 *		query="SELECT OBJECT(e) FROM Version e"
 *		result-type-mapping="Local"
 * 
 * @author Thomas
 */
public abstract class VersionBean extends EntityAdapter implements EntityBean {
	/**
	 * @ejb.create-method
	 */
	public Long ejbCreate(Long id) throws CreateException {
		setId(id);
		return id;
	}

	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="id"
	 */
	public abstract Long getId();

	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="id"
	 */
	public abstract void setId(Long id);

	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="date"
	 */
	public abstract Date getDate();

	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="date"
	 */
	public abstract void setDate(Date date);
	
	/**
	 * @ejb.interface-method view-type="local"
	 *
	 * @ejb.relation name="Version-to-Entry"
	 *		role-name="Version-has-one-entry"
	 *		target-role-name="Entry-has-many-versions"
	 *
	 * @jboss.relation fk-column="id_entry"
	 *		related-pk-field="id"
	 */
	public abstract EntryLocal getEntry();
	
	/**
	 * @ejb.interface-method
	 */
	public abstract void setEntry(EntryLocal entry);

	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="author"
	 */
	public abstract String getAuthor();
	
	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="content"
	 */
	public abstract void setAuthor(String author);
  
  /**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="id_multiversion"
	 */
	public abstract Long getMultiversion();
	
	/**
	 * @ejb.persistent-field
	 * @ejb.interface-method view-type="local"
	 * @ejb.persistence column-name="id_multiversion"
	 */
	public abstract void setMultiversion(Long multiversion);
  
	/**
	 * @ejb.interface-method view-type="local"
	 */
	public VersionLite getVersionLite() {
		VersionLite version = new VersionLite();

		version.setId(getId());
		version.setDate(getDate());
		try {
			version.setAuthor(new User(getAuthor()));	
		} catch(Exception e) {
			// The user could not be retrieved
			version.setAuthor(null);
		}
		version.setMultiversion(getMultiversion());
		
		return version;
	}
}
